package com.chenchao.collection.stream;

import java.util.ArrayList;

/**
 * @encoding : UTF-8
 * @Time : 2020/12/4 20:34
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.collection.stream.Test05.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * sort()排序
 */
public class Test05 {
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("pears");
        strings.add("apple");
        strings.add("apples");
        strings.add("cat");
        /*字母优先*/
        strings.stream().sorted().forEach(System.out::println);
        System.out.println("================");

        /*长度优先，同长度字母优先*/
        /*sorted（）的参数是comparator函数式接口的实现对象*/
        /*comparator 唯一接口compareTo()*/
        strings.stream().sorted((s1,s2)->
            s1.length()-s2.length()==0? s1.compareTo(s2):s1.length()-s2.length()
        ).forEach(System.out::println);
        //s1.compareTo(s2)

    }
}
